#include "regislogin.h"

list<string> list_name;//存放已经登录的用户名  
//接收客户端发来的注册消息
//判断注册名是否重复，并回复是否注册成功
void accept_regis(int socket_client)
{
    ONE:
    char buf[128] = {0};
    char namepsword[128] = {0};
    char npdep[128] = {0};

    recv(socket_client, namepsword, sizeof(namepsword), 0);
    cout<<"regis:"<<namepsword<<endl;
    strcpy(npdep, namepsword);
    char *name = strtok(namepsword, "@");
    //打开存放的 np.txt 判断用户名是否重复 如果重复拒接注册 反之提示注册成功
    FILE *file = fopen("./np.txt", "r+");
    if(file == NULL)
    {
        perror("open file fail");
        return ;
    }
    //判断接收的用户名是否被占用
    while(1)
    {
        bzero(buf ,128);
        //一次读取一行文件内容
        char *p = fgets(buf, 128, file);
        if(p == NULL)
        {
            //遍历完了，说明没有重复的用户名，注册成功，将用户名，密码和胜场存进 np.txt
            char info[] = {"yes"};
            int win_num = 0;
            
            fprintf(file, "%s$%d\n", npdep, win_num);
            send(socket_client, info, sizeof(info), 0);
            break;
        }
        char *oldname = strtok(buf, "@");
        //判断用户注册的名字是否重复
        if(strcmp(oldname, name) == 0)
        {
            char info[] = {"no"};
            //发送消息给客户端，提示注册失败，并且回到开头
            send(socket_client, info, sizeof(info), 0);
            goto ONE;
        }
    }
    fclose(file);
}
//接收客户端发来的登录请求
void accept_login(struct playdata* playdata)
{
    AGIAN:
    char oldbuf[128] = {0};//存放读取出来的用户名和密码
    char buf[128] = {0};
    char namepassword[128] = {0};//存放接收的用户名和密码
    string n;
    recv(playdata->cilentfd, namepassword, sizeof(namepassword), 0);
    cout<<"请求登录:"<<namepassword<<endl;

    strcpy(buf, namepassword);
    char *name = strtok(buf, "@");//将用户名拆分下来

    //将已经登录的用户名遍历，查看是否重复登录
    n = name;
    auto it = list_name.begin();
    for(; it != list_name.end(); ++it)
    {
        cout<<"已经登录的name it:"<<*it<<endl;
        if(*it == n)
        {
            char info[128] = {"repetition"};
            send(playdata->cilentfd, info, sizeof(info), 0);//发送repetition 拒接客户端登录
            cout<<info<<endl;
            goto AGIAN;
        }
    }cout<<endl;

    FILE *file = fopen("./np.txt", "r+");
    if(file == NULL)
    {
        perror("open file fail");
        return ;
    }
    //遍历 np.txt 查看用户名和密码是否正确
    fseek(file, 0, SEEK_SET);
    while(1)
    {
        bzero(oldbuf, 128);
        char buff[128] = {0};
        char *p = fgets(oldbuf, 128, file);
        strcpy(buff, oldbuf);
        char *namepsword = strtok(oldbuf, "$");
        // cout<<namepsword<<" strlen:"<<strlen(namepsword)<<endl;
        if(p == NULL)//说明没有该用户，登录失败
        {
            char info[128] = {"no"};
            send(playdata->cilentfd, info, sizeof(info), 0);
            goto AGIAN;
        }
        else if(strcmp(namepsword, namepassword) == 0)//用户名 密码正确
        {
            //登录成功
            char info[128] = {"yes"};
            send(playdata->cilentfd, info, sizeof(info), 0);
            //cout<<"buff:"<<buff<<endl;

            char buf2[128] = {0};
            char arr[] = {"@ $"};
            char *win = strtok(buff, arr);
            //cout<<win<<endl;
            win = strtok(NULL, arr);
            win = strtok(NULL, arr);
            //cout<<"胜场win"<<win<<endl;
            //将用户名和胜场赋值给 结构体playdata
            playdata->win = atoi(win);
            playdata->name = name;

            //将登录成功的用户名存进链表
            list_name.push_back(name);
            cout<<"登录成功的name:"<<name<<endl;
            break; 
        }
    }
}